//
//  EditBatteryInfoViewController.swift
//  ezyform
//
//  Created by Moennig Q on 14/9/2022.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
class EditBatteryInfoViewController: BaseViewController {
    private lazy var disposeBag = DisposeBag()
    let customView = EditBatteryInfoView()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.initUI()
        let listDeepCopy = JobDetailModelHelper.shared.jobDetailModel.system_detail.batteries.clone()
        self.customView.setData(listDeepCopy, isBattery: JobDetailModelHelper.shared.jobDetailModel.system_detail.is_battery)
        
    }
    func initUI(){
        self.view = self.customView
        self.customView.setTitleInfo(R.string.localizable.editBatteryInformation())
        //MARK: - save 事件
        self.customView.saveBtn.rx.tap.subscribe(onNext: { [weak self] _ in
            guard let strongSelf = self else { return  }
            strongSelf.customView.endEditing(true)
            strongSelf.saveBatteryInfo()
        }).disposed(by: self.disposeBag)
        //MARK: - 增加一行Panel
        self.customView.addBtn.rx.tap.subscribe { [weak self] _ in
            guard let strongSelf = self else { return  }
            strongSelf.customView.contentView.addOneBattery()
            strongSelf.customView.layoutIfNeeded()//会强制重绘并等待完成。
            //table view加载完，跳转到最后新增加的item的位置
            let recOnTableView = strongSelf.customView.contentView.tableView.rectForRow(at: IndexPath(row: strongSelf.customView.contentView.list.count - 1, section: 0))
            let cellPointOnScrollView = strongSelf.customView.contentView.tableView.convert(recOnTableView.origin, to: strongSelf.customView.contentView.scrollView)
            strongSelf.customView.contentView.scrollView.scrollToView(point: cellPointOnScrollView, animated: true)
        }.disposed(by: self.disposeBag)
    }
    
    func saveBatteryInfo(){
        let vm = EditJobInfoService()
        vm.setBlockAfterNetwork { [weak self] (result , _) in
            guard let strongSelf = self else {return}
            ModelViewUtil.shared.hideUpdatingView()
            if let data = result as? PVDDetailModel{
                JobDetailModelHelper.shared.jobDetailModel.system_detail = data.system_detail
            }
            strongSelf.navigationController?.popViewController(animated: true)
        } errorBlock: { (code, error) in
            ModelViewUtil.shared.hideLoadingView()
            Tool().showErrorInfo(code, error)
        }
        
        ModelViewUtil.shared.showLoadingView(R.string.localizable.updating())
        vm.updateSystemInfo(batteryList: self.customView.contentView.list + self.customView.contentView.delList,haveBattery: self.customView.contentView.isBattery.value,spvCheck: JobDetailModelHelper.shared.jobDetailModel.spv_check)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: false)
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        DebugLog("\(self) didReceiveMemoryWarning")
    }
}
